﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace WinApp_ResizeImage
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        string filename;
        private string filename_wide;

        public MainWindow()
        {
            InitializeComponent();
            filename = "C:\\Users\\Mahy\\Pictures\\MuslimotequeAssets\\Muslimoteque_Logo.png";
            filename_wide = "C:\\Users\\Mahy\\Pictures\\MuslimotequeAssets\\Muslimoteque_Logo (wide).png";

        }
        private void Image_Loaded(object sender, RoutedEventArgs e)
        {
            // ... Create a new BitmapImage.
             

            
        }

        private void textchanged(object sender, TextChangedEventArgs e)
        {
            BitmapImage b = new BitmapImage();
            BitmapImage c = new BitmapImage();
            c.BeginInit();
            b.BeginInit();
            c.UriSource = new Uri(wide.Text);
            c.EndInit();
            b.UriSource = new Uri(logo.Text);
            b.EndInit();
            image.Source = b;
            wimage.Source = c;
            // ... Get Image reference from sender.

            // ... Assign Source.

        }

        private void resize_BTN_Click(object sender, RoutedEventArgs e)
        {
            filename = logo.Text;
            filename_wide = wide.Text;
            string results = "" ;
            if (squareRadio.IsChecked == true)
            {
                foreach (ImageSize i in ImageSizes.GetImagesSizeForWindowsPhone())
                {
                    ResizeImage(filename, i.width, i.height);
                    results = filename + ": " + i.width + "x" + i.height + Environment.NewLine;
                }
            }
            else
            {
                foreach (ImageSize i in ImageSizes.GetImagesSizeForWindows())
                {
                    ResizeImage(filename_wide, i.width, i.height);
                    results = filename_wide + ": " + i.width + "x" + i.height + Environment.NewLine;
                }
            }

            MessageBox.Show(results);
            
        }

        private static void ResizeImage(string filename, int width, int height)
        {
            string extension = System.IO.Path.GetExtension(filename);
            string rootFilename = System.IO.Path.GetDirectoryName(filename)
                + @"\" + System.IO.Path.GetFileNameWithoutExtension(filename);

            // load the file data
            byte[] imageBytes = LoadImageData(filename);

            // decode the image to the requested width and height
            BitmapSource imageSource = CreateImage(imageBytes, width, height);

            // encode the image using the original format
            byte[] encodedBytes = GetEncodedImageData(imageSource, extension);

            // save the modified image
            filename = rootFilename + width + "x" +height + extension;
            SaveImageData(encodedBytes, filename);

            //MessageBox.Show(("Resized image to {0} x {1} and saved as {2}.\n",
            //    imageSource.PixelWidth, imageSource.PixelHeight, filename), "Error");
            

        }

        private static void SaveImageData(byte[] imageData, string filePath)
        {
            FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(imageData);
            bw.Close();
            fs.Close();
        }

        private static BitmapSource CreateImage(byte[] imageData, int decodePixelWidth, int decodePixelHeight)
        {
            if (imageData == null) return null;

            BitmapImage result = new BitmapImage();
            result.BeginInit();
            if (decodePixelWidth > 0)
            {
                result.DecodePixelWidth = decodePixelWidth;
            }
            if (decodePixelHeight > 0)
            {
                result.DecodePixelHeight = decodePixelHeight;
            }
            result.StreamSource = new MemoryStream(imageData);
            result.CreateOptions = BitmapCreateOptions.None;
            result.CacheOption = BitmapCacheOption.Default;
            result.EndInit();
            return result;
        }

        private static byte[] LoadImageData(string filePath)
        {
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            byte[] imageBytes = br.ReadBytes((int)fs.Length);
            br.Close();
            fs.Close();
            return imageBytes;
        }

        private static byte[] GetEncodedImageData(ImageSource image, string preferredFormat)
        {
            byte[] result = null;
            BitmapEncoder encoder = null;
            switch (preferredFormat.ToLower())
            {
                case ".jpg":
                case ".jpeg":
                    encoder = new JpegBitmapEncoder();
                    break;

                case ".bmp":
                    encoder = new BmpBitmapEncoder();
                    break;

                case ".png":
                    encoder = new PngBitmapEncoder();
                    break;

                case ".tif":
                case ".tiff":
                    encoder = new TiffBitmapEncoder();
                    break;

                case ".gif":
                    encoder = new GifBitmapEncoder();
                    break;

                case ".wmp":
                    encoder = new WmpBitmapEncoder();
                    break;
            }

            if (image is BitmapSource)
            {
                MemoryStream stream = new MemoryStream();
                encoder.Frames.Add(BitmapFrame.Create(image as BitmapSource));
                encoder.Save(stream);
                stream.Seek(0, SeekOrigin.Begin);
                result = new byte[stream.Length];
                BinaryReader br = new BinaryReader(stream);
                br.Read(result, 0, (int)stream.Length);
                br.Close();
                stream.Close();
            }
            return result;
        }

        private void fileDialog_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dl = new Microsoft.Win32.OpenFileDialog();
            dl.DefaultExt = ".png";
            bool? result = dl.ShowDialog();

            if (result == true)
            {
                string filename = dl.FileName;
                logo.Text = filename;
            }
        }
        private void fileDialogWide_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dl = new Microsoft.Win32.OpenFileDialog();
            dl.DefaultExt = ".png";
            bool? result = dl.ShowDialog();

            if (result == true)
            {
                string filename = dl.FileName;
                wide.Text = filename;
            }
        }
    }

    public class ImageSize
    {
        public int width;
        public int height;
    }

    public class ImageSizes
    {
        public static List<ImageSize> GetImagesSizeForWindowsPhone()
        {
            List<ImageSize> _size = new List<ImageSize>();
            _size.Add(new ImageSize
            {
                width = 170 ,
                height = 170
            });

            _size.Add(new ImageSize
            {
                width = 99,
                height = 99
            });
            _size.Add(new ImageSize
            {
                width = 71,
                height = 71
            });
            _size.Add(new ImageSize
            {
                width = 360,
                height = 360
            });
            _size.Add(new ImageSize
            {
                width = 210,
                height = 210
            });
            _size.Add(new ImageSize
            {
                width = 150,
                height = 150
            });
            _size.Add(new ImageSize
            {
                width = 106,
                height = 106
            });
            _size.Add(new ImageSize
            {
                width = 44,
                height = 44
            });
            _size.Add(new ImageSize
            {
                width = 120,
                height = 120
            });
            _size.Add(new ImageSize
            {
                width = 70,
                height = 70
            });


            _size.Add(new ImageSize
            {
                width = 50,
                height = 50
            });
            _size.Add(new ImageSize
            {
                width = 58,
                height = 58
            });

            _size.Add(new ImageSize
            {
                width = 33,
                height = 33
            });
            _size.Add(new ImageSize
            {
                width = 24,
                height = 24
            });
            _size.Add(new ImageSize
            {
                width = 62,
                height = 62
            });
            _size.Add(new ImageSize
            {
                width = 126,
                height = 126
            });
            _size.Add(new ImageSize
            {
                width = 98,
                height = 98
            });
            _size.Add(new ImageSize
            {
                width = 70,
                height = 70
            });
            _size.Add(new ImageSize
            {
                width = 56,
                height = 56
            });

            _size.Add(new ImageSize
            {
                width = 270,
                height = 270
            });
            _size.Add(new ImageSize
            {
                width = 210,
                height = 210
            }); _size.Add(new ImageSize
            {
                width = 150,
                height = 150
            }); _size.Add(new ImageSize
            {
                width = 120,
                height = 120
            }); _size.Add(new ImageSize
            {
                width = 558,
                height = 558
            }); _size.Add(new ImageSize
            {
                width = 434,
                height = 434
            });

            _size.Add(new ImageSize
            {
                width = 310,
                height = 310
            }); _size.Add(new ImageSize
            {
                width = 248,
                height = 248
            }); _size.Add(new ImageSize
            {
                width = 54,
                height = 54
            }); _size.Add(new ImageSize
            {
                width = 42,
                height = 42
            });

            _size.Add(new ImageSize
            {
                width = 30,
                height = 30
            }); _size.Add(new ImageSize
            {
                width = 24,
                height = 24
            }); _size.Add(new ImageSize
            {
                width = 256,
                height = 256
            }); _size.Add(new ImageSize
            {
                width = 48,
                height = 48
            }); _size.Add(new ImageSize
            {
                width = 32,
                height = 32
            }); _size.Add(new ImageSize
            {
                width = 90,
                height = 90
            }); _size.Add(new ImageSize
            {
                width = 16,
                height =16
            }); _size.Add(new ImageSize
            {
                width = 70,
                height = 70
            }); _size.Add(new ImageSize
            {
                width = 43,
                height = 43
            }); _size.Add(new ImageSize
            {
                width = 50,
                height = 50
            });
            _size.Add(new ImageSize
            {
                width = 43,
                height = 43
            });
            _size.Add(new ImageSize
            {
                width = 33,
                height = 33
            });
            _size.Add(new ImageSize
            {
                width = 24,
                height = 24
            });
            



            return _size;

        }

        internal static IEnumerable<ImageSize> GetImagesSizeForWindows()
        {
            List<ImageSize> _size = new List<ImageSize>();
            _size.Add(new ImageSize
            {
                width = 744,
                height = 360
            });
            _size.Add(new ImageSize
            {
                width = 434,
                height = 210
            });
            _size.Add(new ImageSize
            {
                width = 310,
                height = 150
            });

            _size.Add(new ImageSize
            {
                width = 558,
                height = 270
            });
            _size.Add(new ImageSize
            {
                width = 434,
                height = 210
            }); _size.Add(new ImageSize
            {
                width = 310,
                height = 150
            });
            _size.Add(new ImageSize
            {
                width = 248,
                height = 120
            });


            return _size;
        }
    }
}
